package com.itheima.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.City;
import com.itheima.domain.Pro;
import com.itheima.service.ProService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/province/*")
public class ProvinceServlet extends BaseServlet {
    ProService service = new ProService();
    protected void test1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("execute");
        resp.setContentType("text/json;charset=UTF-8");

        List<Pro> list = service.findAll();
        //resp.getWriter().write("你好");
        ObjectMapper mapper = new ObjectMapper();
        String josn = mapper.writeValueAsString(list);
        resp.getWriter().write(josn);
    }


    protected void city(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=UTF-8");
        String pid = req.getParameter("pid");
        List<City> list = service.findByPid(pid);
        ObjectMapper mapper = new ObjectMapper();
        String josn = mapper.writeValueAsString(list);
        resp.getWriter().write(josn);
    }
}
